function paddZero(n) {
    return n < 10 ? "0" + n : n;
}

//时间格式
function ftime(time) {
    let d = new Date(time);
    let year = paddZero(d.getFullYear());
    let month = paddZero(d.getMonth() + 1);
    let day = paddZero(d.getDay());
    let h = paddZero(d.getHours());
    let m = paddZero(d.getMinutes());
    let s = paddZero(d.getSeconds());

    return `${year}-${month}-${day} ${h}:${m}:${s}`;
}

let id = new URLSearchParams(location.search).get('id');

//评论列表
function links() {
    $.ajax({
        url: `http://localhost:8888/api/articles/${id}/comments`,
        data: id,
        success: function (res) {
            if (res.status === 0) {
                let arr = ["<h4><i class=\"sprites\"></i>评论区</h4>"];
                res.data.forEach(item => {
                    item.cdate = ftime(item.cdate);
                    arr.push(`<div class="kr_comment_card">
                        <div class="img-wrap">
                          <img src="./uploads/avatar_3.jpg" alt="">
                        </div>
                        <div class="info">
                          <p>${item.uname} <span>${item.cdate}</span></p>
                          <p>${item.content}</p>
                        </div>
                        <a href="javascript:;" class="like">${item.count}</a>
                      </div>`);
                })
                $(".Comment-area").html(arr.join(""));
            }
        }
    })
}
//添加评论
$(".Comment-form").submit(function (e) {
    e.preventDefault()
    let data = $(this).serialize()
    console.log(data);
    $.ajax({
        type: "post",
        url: "http://localhost:8888/api/articles/"+id+"/comments",
        data: data,
        success: function (res) {
            console.log(res)
            if (res.status === 0) {
                layer.msg(res.message);
                $(".Comment-form")[0].reset();

                links();
            }
        }
    })
})

links();